package com.tanhua.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.PeachBlossomApi;
import com.tanhua.dubbo.server.pojo.Sound;
import com.tanhua.server.vo.PeachBlossomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
@Slf4j
public class SoundService {

    @Autowired
    protected FastFileStorageClient storageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;
    @Autowired
    private UserInfoService userInfoService;

    @Reference(version = "1.0.0")
    private PeachBlossomApi peachBlossomApi;

    public Boolean sendSound(MultipartFile soundFile) {
        User user = UserThreadLocal.get();

        Sound sound = new Sound();
        sound.setUserId(user.getId());
        try {
            //上传音频到FastDFS中
            StorePath storePath = this.storageClient.uploadFile(soundFile.getInputStream(),
                    soundFile.getSize(),
                    StrUtil.subAfter(soundFile.getOriginalFilename(), '.', true),
                    null);
            sound.setSoundUrl(fdfsWebServer.getWebServerUrl() + storePath.getFullPath());


            String soundId = this.peachBlossomApi.sendSound(sound);
            return StrUtil.isNotEmpty(soundId);
        } catch (IOException e) {
            log.error("上传语音出错~ userId = " + user.getId() + ", file = " + soundFile.getOriginalFilename(), e);
        }
        return false;
    }

    public PeachBlossomVo getSound() {
        User user = UserThreadLocal.get();
        try {
            //查询今日剩余次数
            Long count = peachBlossomApi.queryUserListenerCount(user.getId().toString());
            //判断今日传音次数
            if(count == null || count > 8){
                return null;
            }
            PeachBlossomVo peachBlossomVo = new PeachBlossomVo();
            peachBlossomVo.setRemainingTimes(Convert.toInt(7-count));

            //查询语音
            Sound sound = peachBlossomApi.querySoundRandom(user.getId());
            Long userId = sound.getUserId();
            peachBlossomVo.setId(Convert.toInt(userId));
            //查询用户信息
            UserInfo userInfo = userInfoService.queryUserInfoByUserId(userId);
            peachBlossomVo.setAvatar(userInfo.getLogo());
            //年龄超过四十按三十九处理
            Integer age = userInfo.getAge() > 40 ? 39 : userInfo.getAge();
            peachBlossomVo.setAge(age);
            peachBlossomVo.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
            peachBlossomVo.setNickname(userInfo.getNickName());
            peachBlossomVo.setSoundUrl(sound.getSoundUrl());
            return peachBlossomVo;
        } catch (Exception e) {
            log.error("上传语音出错~ userId = " + user.getId(),e);
        }
        return null;
    }
}
